<!--页面主要内容-->
<main class="lyear-layout-content">
      
    <div class="container-fluid p-t-15">
      
      <div class="row">
        <div class="col-lg-12">
          <div class="card">
            <div class="card-toolbar d-flex flex-column flex-md-row">
              <div class="toolbar-btn-action">
                <a class="btn btn-danger dels" ><i class="mdi mdi-window-close"></i> 删除</a>
              </div>
              
              <form class="search-bar ml-md-auto" method="get" role="form">
                <input type="hidden" name="search_field" id="search-field" value="code" />
                <div class="input-group ml-md-auto">
                  <div class="input-group-prepend">
                    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="search-btn">订单号</button>
                    <div class="dropdown-menu" style="">
                      <a class="dropdown-item" data-field="code">订单号</a>
                    </div>
                  </div>
                  <input type="text" class="form-control" name="keyword" placeholder="{$keyword}">
                </div>
              </form>
            </div>
            <div class="card-body">
              
              <div class="table-responsive">
                <table class="table table-bordered">
                  <thead>
                    <tr>
                      <th>
                        <div class="custom-control custom-checkbox">
                          <input type="checkbox" class="custom-control-input" id="check-all">
                          <label class="custom-control-label" for="check-all"></label>
                        </div>
                      </th>
                      <th>编号</th>
                      <th>订单号</th>
                      <th>用户</th>
                      <th>订单描述</th>
                      <th>配送公司</th>
                      <th>配送单号</th>
                      <th>创建时间</th>
                      <th>状态</th>
                      <th>操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    {foreach $OrderList as $item}
                    <tr>
                      <td>
                        <div class="custom-control custom-checkbox">
                          <input type="checkbox" class="custom-control-input ids" name="ids[]" value="{$item.id}" id="ids-{$item.id}">
                          <label class="custom-control-label" for="ids-{$item.id}"></label>
                        </div>
                      </td>
                      <td>{++$num}</td>
                      <td>{$item.code}</td>
                      <td>{$item.user.nickname}</td>
                      <td>{$item.content}</td>
                      <td>{$item.express.name ? $item.express.name : '未配送'}</td>
                      <td>{$item.expcode ? $item.expcode : '未填写'}</td>
                      <td>{$item.createtime|date="Y-m-d"}</td>
                      <td>{$item.status_text}</td>
                      <td>
                        <div class="btn-group">
                          {if $item.status == 1}
                            <a class="btn btn-xs btn-default express" data-id="{$item.id}"  data-toggle="tooltip" data-original-title="发货"><i class="mdi mdi-pencil"></i></a>
                          {/if}
                          {if $item.status > 1 && $item.status < 3}
                            <a class="btn btn-xs btn-default expre" data-id="{$item.id}"  data-toggle="tooltip" data-original-title="编辑配送单号"><i class="mdi mdi-pencil"></i></a>
                          {/if}

                          {if $item.status != 1}
                          <a class="btn btn-xs btn-default" href="{:url('admin/order/logistics',['orderid' => $item.id])}"  data-toggle="tooltip" data-original-title="查看物流"><i class="mdi mdi-pencil"></i></a>
                          {/if}

                          {if $item.status == -2}
                          <a class="btn btn-xs btn-default success" data-id="{$item.id}"  data-toggle="tooltip" data-original-title="同意退货"><i class="mdi mdi-pencil"></i></a>
                          <a class="btn btn-xs btn-default back" data-id="{$item.id}"  data-toggle="tooltip" data-original-title="拒绝退货"><i class="mdi mdi-pencil"></i></a>
                          {/if}

                          <a class="btn btn-xs btn-default" href="{:url('admin/order/info',['orderid' => $item.id])}" title="" data-toggle="tooltip" data-original-title="查看"><i class="mdi mdi-file-eye"></i></a>
                          <a class="btn btn-xs btn-default del" data-id="{$item.id}" data-toggle="tooltip" data-original-title="删除"><i class="mdi mdi-window-close"></i></a>
                        </div>
                      </td>
                    </tr>
                    {/foreach}
                  </tbody>
                </table>
              </div>
              {$OrderList|raw}
     
            </div>
          </div>
        </div>
        
      </div>
      
    </div>
  
  </main>
  <!--End 页面主要内容-->
  <script>
    $(function () {
      $('.del').click(function () {
        $.confirm({
          title: '删除',
          content: '确认删除？',
          buttons: {
            confirm: {
              text: '确认',
              btnClass: 'btn-info',
              action: () => {
                // 获取要删除的id
                let orderid = $(this).data('id')

                $.ajax({
                  type: 'post',
                  url: `{:url('admin/order/del')}`,
                  data: {
                    orderid
                  },
                  dataType: 'json',
                  success: (result) => {
                    if (result.code === 1) {
                      $.notify({
                        message: result.msg,
                      }, {
                        delay: 1000,
                        type: 'success',
                        onClose: function () {
                          location.reload();
                        }
                      })
                    } else {
                      $.notify({
                        message: result.msg,
                      }, {
                        delay: 1000,
                        type: 'warning'
                      })
                    }
                  }
                })


              }
            },
            cancel: {
              text: '取消',
              btnClass: 'btn-danger'
            }
          }
        })
      })


      $('.dels').click(function () {
        $.confirm({
          title: '批量删除',
          content: '确认批量删除？',
          buttons: {
            confirm: {
              text: '确认',
              btnClass: 'btn-info',
              action: () => {
                // 获取要删除的id
                let orderid = GetId()

                $.ajax({
                  type: 'post',
                  url: `{:url('admin/order/del')}`,
                  data: {
                    orderid
                  },
                  dataType: 'json',
                  success: (result) => {
                    if (result.code === 1) {
                      $.notify({
                        message: result.msg,
                      }, {
                        delay: 1000,
                        type: 'success',
                        onClose: function () {
                          location.reload()
                        }
                      })
                    } else {
                      $.notify({
                        message: result.msg,
                      }, {
                        delay: 1000,
                        type: 'warning'
                      })
                    }
                  }
                })


              }
            },
            cancel: {
              text: '取消',
              btnClass: 'btn-danger'
            }
          }
        })

      })

      var express = [];

      $.ajax({
        type:'post',
        url:`{:url('admin/order/expre')}`,
        dataType:'json',
        success:function(res)
        {
          if(res.code === 1)
          {
            express = res.data
          }
        }
      })

      // 配送
      $('.express').click(function(){
        let orderid = $(this).data('id')
        var option = ''

        if(express)
        {
          express.forEach(item => {
            option += `<option value="${item.id}">${item.name}</option>`
          })
        }
        // 弹窗
        $.confirm({
          title:'配送信息',
          content:`
            <div class="form-group p-1 mb-0">
              <label class="control-label">配送公司</label>
              <div class="form-controls">
                    <select name="express" class="form-control" id="express">
                      <option value="0">请选择</option>
                      ${option}
                    </select>
                  </div>
              <input autofocus="" type="text" id="input-name" placeholder="请输入配送单号" class="form-control">
            </div>
          `,
          buttons:{
            confirm:{
              text: '确认',
              btnClass: 'btn-info',
              action:function() {
                let input = this.$content.find('input#input-name')

                let select = this.$content.find('select#express')

                let selected = select.val()

                let expcode = input.val()

                if(!$.trim(expcode))
                {
                  $.notify({
                      message: '配送单号不能为空',
                  }, {
                      delay: 1000,
                      type: 'warning'
                  })
                  return false
                }

                if(selected == 0 || !selected)
                {
                  $.notify({
                      message: '请选择配送公司',
                  }, {
                      delay: 1000,
                      type: 'warning'
                  })
                  return false
                }

                // 发起请求
                $.ajax({
                  type:'post',
                  url:`{:url('admin/order/express')}`,
                  data:{
                    expcode,
                    orderid,
                    selected
                  },
                  dataType:'json',
                  success:function(res)
                  {
                    if(res.code === 1)
                    {
                      $.notify({
                          message: res.msg,
                      }, {
                          delay: 1000,
                          type: 'success',
                          onClose: function () {
                              location.reload();
                              return false
                          }
                          
                      })
                    }else{
                      $.notify({
                          message: res.msg,
                      }, {
                          delay: 1000,
                          type: 'warning'
                      })
                    }
                  }
                })

              }
            },
            
            cancel: {
                text: '取消',
                btnClass: 'btn-danger'
            }
          }
        })
      })

      var order = []

      let orderid = $('.expre').data('id')

      $.ajax({
        type:'post',
        url:`{:url('admin/order/order')}`,
        data:{
          orderid
        },
        dataType:'json',
        success:function(res)
        {
          if(res.code === 1)
          {
            order = res.data
          }
        }
      })

      // 配送
      $('.expre').click(function(){
        
        var option = ''
        

        console.log(order)

        if(express)
        {
          express.forEach(item => {
            option += `<option value="${item.id}" ${item.id == order.expid ? 'selected' : ''}>${item.name}</option>`
          })
        }
        // 弹窗
        $.confirm({
          title:'修改配送信息',
          content:`
            <div class="form-group p-1 mb-0">
              <label class="control-label">配送公司</label>
              <div class="form-controls">
                    <select name="express" class="form-control" id="express">
                      <option value="0">请选择</option>
                      ${option}
                    </select>
                  </div>
              <input autofocus="" type="text" id="input-name" placeholder="请输入配送单号" value="${order.expcode}" class="form-control">
            </div>
          `,
          buttons:{
            confirm:{
              text: '确认',
              btnClass: 'btn-info',
              action:function() {
                let input = this.$content.find('input#input-name')

                let select = this.$content.find('select#express')

                let selected = select.val()

                let expcode = input.val()

                if(!$.trim(expcode))
                {
                  $.notify({
                      message: '配送单号不能为空',
                  }, {
                      delay: 1000,
                      type: 'warning'
                  })
                  return false
                }

                if(selected == 0 || !selected)
                {
                  $.notify({
                      message: '请选择配送公司',
                  }, {
                      delay: 1000,
                      type: 'warning'
                  })
                  return false
                }

                // 发起请求
                $.ajax({
                  type:'post',
                  url:`{:url('admin/order/express')}`,
                  data:{
                    expcode,
                    orderid,
                    selected,
                    action:'edit'
                  },
                  dataType:'json',
                  success:function(res)
                  {
                    if(res.code === 1)
                    {
                      $.notify({
                          message: res.msg,
                      }, {
                          delay: 1000,
                          type: 'success',
                          onClose: function () {
                              location.reload();
                              return false
                          }
                          
                      })
                    }else{
                      $.notify({
                          message: res.msg,
                      }, {
                          delay: 1000,
                          type: 'warning'
                      })
                    }
                  }
                })

              }
            },
            
            cancel: {
                text: '取消',
                btnClass: 'btn-danger'
            }
          }
        })
      })

      $('.success').click(function(){

          $.confirm({
          title: '退货？',
          content: '确认同意退货？',
          buttons: {
            confirm: {
              text: '确认',
              btnClass: 'btn-info',
              action: () => {
                // 获取要删除的id
                let orderid = $(this).data('id')
                console.log(orderid)

                $.ajax({
                  type:'post',
                  url:`{:url('admin/order/back')}`,
                  data:{
                    orderid,
                    action:'success'
                  },
                  dataType:'json',
                  success:function(res)
                  {
                    if(res.code === 1)
                    {
                      $.notify({
                          message: res.msg,
                      }, {
                          delay: 1000,
                          type: 'success',
                          onClose: function () {
                              location.reload();
                              return false
                          }
                          
                      })
                    }else{
                      $.notify({
                          message: res.msg,
                      }, {
                          delay: 1000,
                          type: 'warning'
                      })
                    }
                  }
                })


              }
            },
            cancel: {
              text: '取消',
              btnClass: 'btn-danger'
            }
          }
        })
      })

      $('.back').click(function(){

        $.confirm({
          title: '退货？',
          content: '确认拒绝退货？',
          buttons: {
            confirm: {
              text: '确认',
              btnClass: 'btn-info',
              action: () => {
                // 获取要删除的id
                let orderid = $(this).data('id')

                $.ajax({
                  type:'post',
                  url:`{:url('admin/order/back')}`,
                  data:{
                    orderid,
                  },
                  dataType:'json',
                  success:function(res)
                  {
                    if(res.code === 1)
                    {
                      $.notify({
                          message: res.msg,
                      }, {
                          delay: 1000,
                          type: 'success',
                          onClose: function () {
                              location.reload();
                              return false
                          }
                          
                      })
                    }else{
                      $.notify({
                          message: res.msg,
                      }, {
                          delay: 1000,
                          type: 'warning'
                      })
                    }
                  }
                })


              }
            },
            cancel: {
              text: '取消',
              btnClass: 'btn-danger'
            }
          }
        })
      })


    })
  </script>